using Gateway.Common.Extensions;
using Gateway.Domain.EFCore.Configuration;
using Gateway.Domain.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace Gateway.Domain.Configuration;

public class UserConfiguration : AggregateRootEntityConfiguration<User>
{
    public override void Configure(EntityTypeBuilder<User> builder)
    {
        builder.ToTable(t => t.HasComment("用户表"));
        base.Configure(builder);
        builder.Property(x => x.Account).IsRequired().HasMaxLength(50).HasComment("账号");
        builder.Property(x => x.UserName).IsRequired().HasMaxLength(50).HasComment("姓名");

        builder.Property(x => x.UserNo).IsRequired().HasMaxLength(50).HasComment("用户编号");
        builder.Property(x => x.UserAvatar).HasMaxLength(200).HasComment("用户头像");

        builder.Property(x => x.Password).IsRequired().HasMaxLength(100).HasComment("密码");
        builder.Property(x => x.DepartmentId).HasMaxLength(36).HasComment("部门标识");
        builder.Property(x => x.RoleIds).HasMaxLength(500).HasComment("用户角色")
            .HasConversion(x => string.Join(",", x), s => s.ToSplitList(","));
        // 初始化 数据
        var user = new User("admin", "", "", "123456", new List<string>() { "default" }, "admin");
        user.SetCreate("admin", "admin");
        user.SetDefaultId("admin");
        builder.HasData(user);
    }
}